package com.ruoyi.kong.service.impl;

import com.ruoyi.kong.entity.*;
import com.ruoyi.kong.fegin.KongClientService;
import com.ruoyi.kong.service.IKongServiceInfoService;
import com.ruoyi.kong.utils.KongActiveProfileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * kong 代理接口实现类
 */
@Service
public class KongServiceInfoServiceImpl implements IKongServiceInfoService {

    private String baseUrl = "";
    /**
     *
     */
    @Autowired
    private KongActiveProfileUtil kongActiveProfileUtil;


    /**
     * 请求kong 网关 返回kong的信息
     *
     * @return
     */
    public Map getKongInfos() {
        String baseUrl = kongActiveProfileUtil.getKongServerUrl();
        return KongClientService.getInstance(baseUrl).kongInfo();
    }

    /**
     * 请求kong 网关返回kong 的 状态信息
     *
     * @return
     */
    public Map getKongStatus() {
        String baseUrl = kongActiveProfileUtil.getKongServerUrl();
        return KongClientService.getInstance(baseUrl).kongStatus();
    }

    /**
     * 返回 kong 所有 服务信息
     *
     * @return
     */
    @Override
    public KongEntity<KongServiceResult> getKongServiceList() {
        KongEntity<KongServiceResult> kongServiceInfos = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getKongServiceInfos();
        if(!ObjectUtils.isEmpty(kongServiceInfos)){
            for (KongServiceResult serviceResult : kongServiceInfos.getData()){
                String serviceId= serviceResult.getId();
                KongEntity<KongRoute> kongRoutes = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).routeByServiceId(serviceId);
                serviceResult.setKongRoutes(kongRoutes.getData());
                KongEntity<KongPlugin> kongPlugins = KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).pluginsByServiceId(serviceId);
                serviceResult.setKongPlugins(kongPlugins.getData());
            }
        }
        return kongServiceInfos;
    }


    /**
     * 根据kong的服务id 返回服务信息
     * @param serviceId
     * @return
     */
    public KongService getKongServiceById(String serviceId) {
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getServiceById(serviceId);
    }


    /**
     * 根据路由id 返回 服务信息
     * @param routeId
     * @return
     */
    public KongService getKongServiceByRouteId(String routeId){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getServiceByRouteId(routeId);
    }


    /**
     * 根据控件id 返回服务信息
     * @param pluginId
     * @return
     */
    public KongService getKongServiceBuPluginId(String pluginId){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).getServiceByPluginId(pluginId);
    }


    /**
     * 新增服务
     * @param kongService
     * @return
     */
    public KongEntity<KongService> addKongService(KongService kongService){
        return KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).addService(kongService);
    }


    public void deleteServices(String serviceId){
         KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).deleteServices(serviceId);
    }


    public  KongEntity<KongService> updateServices(String serviceId, KongService kongService){
        return  KongClientService.getInstance(kongActiveProfileUtil.getKongServerUrl()).updateServices(serviceId,kongService);
    }


}
